package com.nhc.model;

import android.content.Context;
import android.content.SharedPreferences;

import zuo.biao.library.util.JSON;

public class CoordParmFileHandle {

    private static CoordParmFileHandle instance = new CoordParmFileHandle();

    public static CoordParmFileHandle getInstance() {
        return instance;
    }

    public CoordParmInfo coordParmInfo = new CoordParmInfo();

    public CoordSelfInfo coordSelfInfo = new CoordSelfInfo();
    public CoordSelfInfo obliquemercator = new CoordSelfInfo();//没有用
    public CoordSelfInfo philippineszone1 = new CoordSelfInfo();//没有用
    public CoordSelfInfo philippineszone2 = new CoordSelfInfo();
    public CoordSelfInfo philippineszone3 = new CoordSelfInfo();
    public CoordSelfInfo philippineszone4 = new CoordSelfInfo();
    public CoordSelfInfo philippineszone5 = new CoordSelfInfo();

    public CoordSelfInfo mCoordParmInfoEdit;

    public CoordParmFileHandle() {
    }

    private String COORDPARMINFO = "CoordParmInfo";

    public void save(Context context) {
        if (context == null) {
            return;
        }

        SharedPreferences sp = context.getSharedPreferences(
                COORDPARMINFO, Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = sp.edit();
        editor.putString("coordParmInfo", JSON.toJSONString(coordParmInfo));
        editor.putString("coordSelfInfo", JSON.toJSONString(coordSelfInfo));
        editor.putString("obliquemercator", JSON.toJSONString(obliquemercator));
        editor.putString("philippineszone1", JSON.toJSONString(philippineszone1));
        editor.putString("philippineszone2", JSON.toJSONString(philippineszone2));
        editor.putString("philippineszone3", JSON.toJSONString(philippineszone3));
        editor.putString("philippineszone4", JSON.toJSONString(philippineszone4));
        editor.putString("philippineszone5", JSON.toJSONString(philippineszone5));
        editor.commit();
    }

    public void load(Context context) {
        if (context == null) {
            return;
        }

        SharedPreferences sp = context.getSharedPreferences(
                COORDPARMINFO, Context.MODE_PRIVATE);
        String strcoordParmInfo = sp.getString("coordParmInfo", "");
        String strcoordSelfInfo = sp.getString("coordSelfInfo", "");
        String strobliquemercator = sp.getString("obliquemercator", "");
        String strphilippineszone1 = sp.getString("philippineszone1", "");
        String strphilippineszone2 = sp.getString("philippineszone2", "");
        String strphilippineszone3 = sp.getString("philippineszone3", "");
        String strphilippineszone4 = sp.getString("philippineszone4", "");
        String strphilippineszone5 = sp.getString("philippineszone5", "");

        try {
            if (strcoordParmInfo.length() > 1) {
                com.alibaba.fastjson.JSONObject data = JSON.parseObject(strcoordParmInfo);
                if (data != null) {
                    coordParmInfo = JSON.parseObject(data, CoordParmInfo.class);
                }
            }
            if (strcoordSelfInfo.length() > 1) {
                com.alibaba.fastjson.JSONObject data = JSON.parseObject(strcoordSelfInfo);
                if (data != null) {
                    coordSelfInfo = JSON.parseObject(data, CoordSelfInfo.class);
                }
            }
            if (strobliquemercator.length() > 1) {
                com.alibaba.fastjson.JSONObject data = JSON.parseObject(strobliquemercator);
                if (data != null) {
                    obliquemercator = JSON.parseObject(data, CoordSelfInfo.class);
                }
            }
            if (strphilippineszone1.length() > 1) {
                com.alibaba.fastjson.JSONObject data = JSON.parseObject(strphilippineszone1);
                if (data != null) {
                    philippineszone1 = JSON.parseObject(data, CoordSelfInfo.class);
                }
            }
            if (strphilippineszone2.length() > 1) {
                com.alibaba.fastjson.JSONObject data = JSON.parseObject(strphilippineszone2);
                if (data != null) {
                    philippineszone2 = JSON.parseObject(data, CoordSelfInfo.class);
                }
            }
            if (strphilippineszone3.length() > 1) {
                com.alibaba.fastjson.JSONObject data = JSON.parseObject(strphilippineszone3);
                if (data != null) {
                    philippineszone3 = JSON.parseObject(data, CoordSelfInfo.class);
                }
            }
            if (strphilippineszone4.length() > 1) {
                com.alibaba.fastjson.JSONObject data = JSON.parseObject(strphilippineszone4);
                if (data != null) {
                    philippineszone4 = JSON.parseObject(data, CoordSelfInfo.class);
                }
            }
            if (strphilippineszone5.length() > 1) {
                com.alibaba.fastjson.JSONObject data = JSON.parseObject(strphilippineszone5);
                if (data != null) {
                    philippineszone5 = JSON.parseObject(data, CoordSelfInfo.class);
                }
            }
        } catch (Exception e1) {

        }

    }

    public void setEdit(int index) {
        switch (index) {
            default:
            case 0:
                mCoordParmInfoEdit = coordSelfInfo;
                break;
            case 1:
                mCoordParmInfoEdit = obliquemercator;
                break;
            case 2:
                mCoordParmInfoEdit = philippineszone1;
                break;
            case 3:
                mCoordParmInfoEdit = philippineszone2;
                break;
            case 4:
                mCoordParmInfoEdit = philippineszone3;
                break;
            case 5:
                mCoordParmInfoEdit = philippineszone4;
                break;
            case 6:
                mCoordParmInfoEdit = philippineszone5;
                break;
        }
    }

    //philippineszone 1～5 没有设置页面
    public CoordSelfInfo getPhilippineszone(int index) {
        CoordSelfInfo a = new CoordSelfInfo();
        a.TY = 0.99995;
        a.ZLON = 117 + index * 2;
        return a;
    }
}